Ext.ns('AmHistoryRecordView');

AmHistoryRecordView = Ext.extend(
				Ext.Panel,
				{
					// 头部工具栏
					topbar : null,
					// 条件搜索Panel
					searchPanel : null,
					// 数据展示Panel
					gridPanel : null,
					// GridPanel的数据Store
					store : null,
					// 构造函数
					constructor : function(_cfg) {
						Ext.applyIf(this, _cfg);
						// 初始化组件
						this.initUIComponents();
						// 调用父类构造
						AmHistoryRecordView.superclass.constructor.call(this, {
							id : 'AmHistoryRecordView',
							title : '电表记录查询',
							region : 'center',
							layout : 'border',
							items : [ this.searchPanel, this.gridPanel ]
						});
					},// end of constructor

					// 初始化组件
					initUIComponents : function() {
						// 初始化搜索条件Panel
						this.searchPanel = new Ext.FormPanel(
								{
									id : 'AmHistoryRecordSearchForm',
									height : 120,
									region : 'north',
									frame : false,
									border : false,
									padding : '10',
									layout : 'column',
									title : '查询条件',
									labelWidth : 60,
									fileUpload : true,
									layoutConfig : {
										padding : '5',
										align : 'middle'
									},
									defaults : {
										columnWidth : 0.5,
										layout : 'form',
										border : false,
										style : 'padding-left:10px;',
										defaults : {
											xtype : 'textfield',
											width : 130
										}
									},
									items : [{
												items : [ {
													fieldLabel : '电表编号',
													id :　'pdaAmMeterRecord.amId',
													name : 'Q_amId_S_LK'
												},{
													fieldLabel : '栋',
													hiddenName : 'Q_buildingId_S_LK',
													xtype : 'combo',
													id : 'pdaAmMeterRecord.buildingID',
													valueField : 'buildingId',
													displayField : 'buildingtName',
													mode : 'local',
													editable : false,
													triggerAction : 'all',
													store : new Ext.data.SimpleStore({
																url : __ctxPath+ '/pdaweb/showBuildingAmPlan.do?districtId=',
																//autoLoad : true,
																fields : ['buildingId','buildingtName']
															}),
													listeners : {
														scope : this,
														select : function(cbo,record, index) {
															var h = Ext.getCmp('pdaAmMeterRecord.houseID');
															h.setValue('');
															h.store.loadData([]);
															chooseBulidingId = cbo.getValue();
															Ext.Ajax.request({
																url : __ctxPath+ '/pdaweb/showHouseWaterMeterPlan.do?buildingId='
																		+ chooseBulidingId,
																method : 'post',
																success : function(response,option) {
																	h.store.loadData(eval("("+ response.responseText+ ")"));
																}
															});
														}
													}
												
												
												
												},{
													fieldLabel : '房',
													width : 220,
													hiddenName : 'Q_aparmentId_S_LK',
													xtype : 'combo',
													id : 'pdaAmMeterRecord.houseID',
													valueField : 'houseId',
													displayField : 'houseName',
													mode : 'local',
													editable : false,
													triggerAction : 'all',
													store : new Ext.data.SimpleStore({
																url : __ctxPath+ '/pdaweb/showHouseWaterMeterPlan.do?buildingId=',
																//autoLoad : true,
																fields : ['houseId','houseName']
															}),
													listeners : {
														scope : this,
														select : function(cbo,record, index) {
														}
													}
												
												
												
												}]
											},{
												items : [{

															fieldLabel : '区',
															hiddenName : 'Q_districtId_S_LK',
															xtype : 'combo',
															id : 'pdaAmMeterRecord.districtID',
															valueField : 'districtId',
															displayField : 'districtName',
															mode : 'local',
															editable : false,
															triggerAction : 'all',
															store : new Ext.data.SimpleStore({
																		url : __ctxPath
																				+ '/pdaweb/showDistrictAmPlan.do',
																		autoLoad : true,
																		fields : ['districtId','districtName']
																	}),
															listeners : {
																scope : this,
																select : function(cbo,record, index) {
																	var b = Ext.getCmp('pdaAmMeterRecord.buildingID');
																	var h = Ext.getCmp('pdaAmMeterRecord.houseID');
																	b.setValue('');
																	b.store.loadData([]);
																	h.setValue('');
																	h.store.loadData([]);
																	
																	chooseDistrictId = cbo.getValue();
																	Ext.Ajax.request({
																		url : __ctxPath+ '/pdaweb/showBuildingAmPlan.do?districtId='
																				+ chooseDistrictId,
																		method : 'post',
																		success : function(response,option) {
																			b.store.loadData(eval("("+ response.responseText
																							+ ")"));
																		}
																	});
																}
															}
														},{
															xtype : 'button',
															text : '查询',
															iconCls : 'search',
															width : 60,
															handler : function() {
																var searchPanel = Ext.getCmp('AmHistoryRecordSearchForm');
																var gridPanel = Ext.getCmp('AmHistoryRecordGrid');
																if (searchPanel.getForm().isValid()) {
																	$search({
																		searchPanel : searchPanel,
																		gridPanel : gridPanel
																	});
																}
															}
														},{
															style : 'margin-top:4px;',
															width : 60,
									    					xtype : 'button',
									    					text : '重置',
									    					iconCls : 'btn-reset',
									    					handler : function() {
									    						Ext.getCmp('pdaAmMeterRecord.amId').setValue('');
									    						Ext.getCmp('pdaAmMeterRecord.districtID').setValue('');
									    						Ext.getCmp('pdaAmMeterRecord.buildingID').setValue('');
									    						Ext.getCmp('pdaAmMeterRecord.houseID').setValue('');
									    						//Ext.getCmp('WaterHistoryRecordSearchForm').getForm().reset();
									    					}
														}]
											}]
								});// end of the searchPanel

						this.store = new Ext.data.Store({
							proxy : new Ext.data.HttpProxy({
								url : __ctxPath + '/pdaweb/listPdaAmMeter.do',
								params : {/*
									'Q_type_S_IN' : '1,2,3'
								*/}
							}),
							reader : new Ext.data.JsonReader({
								root : 'result',
								totalProperty : 'totalCounts',
								fields : [ {
									name : 'id',
									type : 'int'
								}, 'amId', 'type', 'districtId', 'buildingId',
										'aparmentId' ]
							}),
							remoteSort : true
						});
						
						this.store.load({
							params : {
								start : 0,
								limit : 15
							}
						});
						// end of the store

						var cm = new Ext.grid.ColumnModel({
							columns : [ new Ext.grid.RowNumberer(), {
								header : '电表编号',
								width : 120,
								dataIndex : 'amId',
								renderer : linkDetail
							}, {
								header : '区号',
								width : 80,
								dataIndex : 'districtId'
							}, {
								header : '栋号',
								width : 120,
								dataIndex : 'buildingId'
							}, {
								header : '房号',
								width : 180,
								dataIndex : 'aparmentId'
							}, {
								header : '狀态',
								width : 90,
								dataIndex : 'type',
								renderer : function(val) {
									if(val=='1'){
										return '<span style="color:#336600;">申请中</span>';
									}else if(val=='2'){
										return '使用中';
									}else if(val=='3'){
										return '<span style="color:#aaaaaa;">已报废</span>';
									}else if(val=='4'){
										return '<span style="color:red;">停用中</span>';
									}
									return "";
								}
							} ],
							defaults : {
								sortable : true,
								menuDisabled : false
							// width : 100
							}
						});// end of the cm

						this.topbar = new Ext.Toolbar({
							id : 'WaterHistorRecordFootBar',
							height : 25,
							bodyStyle : 'text-align:left',
							items : []
						});

						this.gridPanel = new Ext.grid.GridPanel({
							id : 'AmHistoryRecordGrid',
							tbar : this.topbar,
							region : 'center',
							store : this.store,
							trackMouseOver : true,
							disableSelection : false,
							loadMask : true,
							autoHeight : true,
							cm : cm,
							bbar : new Ext.PagingToolbar({
								pageSize : 15,
								store : this.store,
								displayInfo : true,
								displayMsg : '当前显示从{0}至{1}， 共{2}条记录',
								emptyMsg : "当前没有记录"
							})
						});

						var gridPanel = this.gridPanel;
					}
				});

// 显示明细资料
AmHistoryRecordView.detailShow = function(recId, quId, dongId, fangId, type) {
	new AmHistoryDetailRecordForm({
		recId : recId,
		quId : quId,
		dongId : dongId,
		fangId : fangId,
		type : type
	}).show();
};

// 電表編號連接
function linkDetail(value, cellmeta, record, rowIndex) {
	return "<a href='#' onClick=AmHistoryRecordView.detailShow('" + value
			+ "'," + "'" + record.data["districtId"] + "','"
			+ record.data["buildingId"] + "'" + ",'"
			+ record.data["aparmentId"] + "','"+record.data["type"]+"')>" + value + "</a>";

}